/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgRegistroCliente.java
 *
 * Created on Oct 20, 2011, 1:52:17 AM
 */

package visual.Despacho;

import bentity.beCliente;
import bentity.beUsuario;
import blogic.blCliente;
import blogic.blLog;
import javax.swing.JOptionPane;
import visual.Seguridad.visualHelper;

/**
 *
 * @author giuliomv
 */
public class dlgRegistroCliente extends javax.swing.JDialog {
    private beUsuario usuario = null;
    /** Creates new form dlgRegistroCliente */
    public dlgRegistroCliente(pnfMantenimientoClientes parent, boolean modal, beUsuario usuario) {
        super((java.awt.Frame)null, modal);
        initComponents();
        this.tbxRazonSocial.setTransferHandler(null);
        this.tbxDireccion.setTransferHandler(null);
        this.usuario = usuario;
        this.setBounds(0, 0, 500, 420);
        this.setResizable(false);
        visualHelper.centrarDialogoEnPantalla(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnBuscarCliente = new javax.swing.JButton();
        lblTitulo = new javax.swing.JLabel();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        pnlDatosClientes = new javax.swing.JPanel();
        lblRUC = new javax.swing.JLabel();
        tbxRUC = new javax.swing.JTextField();
        tbxTelefono = new javax.swing.JTextField();
        tbxEmail = new javax.swing.JTextField();
        lblTelefono = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        tbxRazonSocial = new javax.swing.JTextField();
        lblRazonSocial = new javax.swing.JLabel();
        tbxContacto = new javax.swing.JTextField();
        lblContacto = new javax.swing.JLabel();
        tbxDireccion = new javax.swing.JTextField();
        lblDireccion = new javax.swing.JLabel();
        lblBusqFila = new javax.swing.JLabel();
        lblBusqFila1 = new javax.swing.JLabel();
        lblBusqFila2 = new javax.swing.JLabel();
        lblBusqFila3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(null);

        btnBuscarCliente.setBackground(new java.awt.Color(240, 235, 226));
        btnBuscarCliente.setFont(new java.awt.Font("Dialog", 0, 12));
        btnBuscarCliente.setForeground(new java.awt.Color(0, 0, 0));
        btnBuscarCliente.setText("Limpiar");
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscarCliente);
        btnBuscarCliente.setBounds(200, 340, 110, 27);

        lblTitulo.setFont(new java.awt.Font("Dialog", 0, 36));
        lblTitulo.setForeground(new java.awt.Color(0, 0, 0));
        lblTitulo.setText("Registro de cliente");
        getContentPane().add(lblTitulo);
        lblTitulo.setBounds(60, 10, 338, 43);

        btnRegistrar.setBackground(new java.awt.Color(240, 235, 226));
        btnRegistrar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnRegistrar.setForeground(new java.awt.Color(0, 0, 0));
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnRegistrar);
        btnRegistrar.setBounds(40, 340, 110, 27);

        btnCancelar.setBackground(new java.awt.Color(240, 235, 226));
        btnCancelar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnCancelar.setForeground(new java.awt.Color(0, 0, 0));
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(350, 340, 100, 27);

        pnlDatosClientes.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Datos de cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14), new java.awt.Color(0, 0, 0))); // NOI18N
        pnlDatosClientes.setLayout(null);

        lblRUC.setFont(new java.awt.Font("Dialog", 0, 12));
        lblRUC.setText("RUC ");
        pnlDatosClientes.add(lblRUC);
        lblRUC.setBounds(20, 70, 40, 15);

        tbxRUC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxRUCKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxRUC);
        tbxRUC.setBounds(120, 70, 260, 28);

        tbxTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxTelefonoKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxTelefono);
        tbxTelefono.setBounds(120, 150, 260, 28);

        tbxEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxEmailKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxEmail);
        tbxEmail.setBounds(120, 190, 260, 28);

        lblTelefono.setFont(new java.awt.Font("Dialog", 0, 12));
        lblTelefono.setForeground(new java.awt.Color(0, 0, 0));
        lblTelefono.setText("Telefono ");
        pnlDatosClientes.add(lblTelefono);
        lblTelefono.setBounds(20, 150, 80, 15);

        lblEmail.setFont(new java.awt.Font("Dialog", 0, 12));
        lblEmail.setForeground(new java.awt.Color(0, 0, 0));
        lblEmail.setText("Email");
        pnlDatosClientes.add(lblEmail);
        lblEmail.setBounds(20, 190, 70, 15);

        tbxRazonSocial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxRazonSocialKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxRazonSocial);
        tbxRazonSocial.setBounds(120, 30, 260, 28);

        lblRazonSocial.setFont(new java.awt.Font("Dialog", 0, 12));
        lblRazonSocial.setForeground(new java.awt.Color(0, 0, 0));
        lblRazonSocial.setText("Razon social ");
        pnlDatosClientes.add(lblRazonSocial);
        lblRazonSocial.setBounds(20, 30, 90, 15);

        tbxContacto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxContactoKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxContacto);
        tbxContacto.setBounds(120, 230, 260, 28);

        lblContacto.setFont(new java.awt.Font("Dialog", 0, 12));
        lblContacto.setForeground(new java.awt.Color(0, 0, 0));
        lblContacto.setText("Contacto");
        pnlDatosClientes.add(lblContacto);
        lblContacto.setBounds(20, 230, 70, 15);

        tbxDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxDireccionKeyTyped(evt);
            }
        });
        pnlDatosClientes.add(tbxDireccion);
        tbxDireccion.setBounds(120, 110, 260, 28);

        lblDireccion.setFont(new java.awt.Font("Dialog", 0, 12));
        lblDireccion.setForeground(new java.awt.Color(0, 0, 0));
        lblDireccion.setText("Dirección ");
        pnlDatosClientes.add(lblDireccion);
        lblDireccion.setBounds(20, 110, 70, 15);

        lblBusqFila.setFont(new java.awt.Font("Dialog", 0, 12));
        lblBusqFila.setForeground(java.awt.Color.red);
        lblBusqFila.setText("(*)");
        pnlDatosClientes.add(lblBusqFila);
        lblBusqFila.setBounds(100, 30, 30, 15);

        lblBusqFila1.setFont(new java.awt.Font("Dialog", 0, 12));
        lblBusqFila1.setForeground(java.awt.Color.red);
        lblBusqFila1.setText("(*)");
        pnlDatosClientes.add(lblBusqFila1);
        lblBusqFila1.setBounds(50, 70, 30, 15);

        lblBusqFila2.setFont(new java.awt.Font("Dialog", 0, 12));
        lblBusqFila2.setForeground(java.awt.Color.red);
        lblBusqFila2.setText("(*)");
        pnlDatosClientes.add(lblBusqFila2);
        lblBusqFila2.setBounds(90, 110, 30, 15);

        lblBusqFila3.setFont(new java.awt.Font("Dialog", 0, 12));
        lblBusqFila3.setForeground(java.awt.Color.red);
        lblBusqFila3.setText("(*)");
        pnlDatosClientes.add(lblBusqFila3);
        lblBusqFila3.setBounds(80, 150, 30, 15);

        getContentPane().add(pnlDatosClientes);
        pnlDatosClientes.setBounds(30, 60, 420, 270);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        // TODO add your handling code here:
        if (    !this.tbxDireccion.getText().isEmpty() &&
                !this.tbxRUC.getText().isEmpty() &&
                !this.tbxRazonSocial.getText().isEmpty() &&
                !this.tbxTelefono.getText().isEmpty()    ){
            if (this.tbxRUC.getText().length() == 11){
                String strRazonSocial = "";
                String strRuc = "";
                String strTelefono = "";
                String strEmail = "";
                String strContacto = "";
                String strDireccion = "";
                try{
                    strRazonSocial = this.tbxRazonSocial.getText();
                    strRuc = this.tbxRUC.getText();
                    strTelefono = this.tbxTelefono.getText();
                    strEmail = this.tbxEmail.getText();
                    if (strEmail.isEmpty())
                        strEmail = "Sin especificar";
                    strContacto = this.tbxContacto.getText();
                    if (strContacto.isEmpty())
                        strContacto = "Sin especificar";
                    strDireccion = this.tbxDireccion.getText();
                    beCliente cliente = new beCliente(-1, strRazonSocial, strRuc, strTelefono, strEmail, strContacto, null, beCliente.C_ACTIVO, strDireccion);
                    blCliente blCliente = new blCliente();
                    blCliente.registrarCliente(cliente);
                    visualHelper.mostrarMensajeSatisfaccion("Se registró satisfactoriamente el cliente "+this.tbxRazonSocial.getText());
                    (new blLog()).registrarLog(this.usuario.getIdUsuario(), this.usuario.getCargo(), "Se registró un cliente "+this.tbxRazonSocial.getText());
                    this.limpiarCampos();
                    this.setVisible(false);
                    this.dispose();
                }
                catch (Exception e){
                    e.printStackTrace();
                    visualHelper.mostrarMensajeError("No se pudo registrar el cliente.");
                }
            }
            else
                visualHelper.mostrarMensajeAdvertencia("El RUC debe tener 11 dígitos.");
        }
        else
            visualHelper.mostrarMensajeAdvertencia("Debe llenar los campos obligatorios (*).");
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        // TODO add your handling code here:
        this.limpiarCampos();
    }//GEN-LAST:event_btnBuscarClienteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        int rslt = visualHelper.mostrarPreguntaSiNo("Los cambios no se guardarán. ¿Desea salir de todos modos?");
        if (rslt == JOptionPane.YES_OPTION){
            this.setVisible(false);
            this.dispose();
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void tbxRUCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxRUCKeyTyped
        // TODO add your handling code here:
        String strRuc = this.tbxRUC.getText();
        if (strRuc.length() == 11)
            visualHelper.ningunCaracterEnTextField(evt);
        else
            visualHelper.soloDigitosEnTextField(evt);
    }//GEN-LAST:event_tbxRUCKeyTyped

    private void tbxTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxTelefonoKeyTyped
        // TODO add your handling code here:
        String cad = this.tbxTelefono.getText();
        if (cad.length() <= 15)
            visualHelper.validarTelefono(evt);
        else
            evt.consume();
    }//GEN-LAST:event_tbxTelefonoKeyTyped

    private void tbxRazonSocialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxRazonSocialKeyTyped
        // TODO add your handling code here:
        String cad = this.tbxRazonSocial.getText();
        if (cad.length() <= 50)
            visualHelper.soloNombres(evt);
        else
            evt.consume();
    }//GEN-LAST:event_tbxRazonSocialKeyTyped

    private void tbxDireccionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxDireccionKeyTyped
        // TODO add your handling code here:
        String cad = this.tbxDireccion.getText();
        if (cad.length() <= 50)
            visualHelper.soloNombres(evt);
        else
            evt.consume();
    }//GEN-LAST:event_tbxDireccionKeyTyped

    private void tbxEmailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxEmailKeyTyped
        // TODO add your handling code here:
        String cad = this.tbxEmail.getText();
        if (cad.length() <= 50)
            visualHelper.soloEmail(evt);
        else
            evt.consume();
    }//GEN-LAST:event_tbxEmailKeyTyped

    private void tbxContactoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxContactoKeyTyped
        // TODO add your handling code here:
        String cad = this.tbxContacto.getText();
        if (cad.length() <= 50)
            visualHelper.soloNombres(evt);
        else
            evt.consume();
    }//GEN-LAST:event_tbxContactoKeyTyped

    public void limpiarCampos(){
        this.tbxContacto.setText("");
        this.tbxEmail.setText("");
        this.tbxRUC.setText("");
        this.tbxRazonSocial.setText("");
        this.tbxTelefono.setText("");
        this.tbxDireccion.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JLabel lblBusqFila;
    private javax.swing.JLabel lblBusqFila1;
    private javax.swing.JLabel lblBusqFila2;
    private javax.swing.JLabel lblBusqFila3;
    private javax.swing.JLabel lblContacto;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblRUC;
    private javax.swing.JLabel lblRazonSocial;
    private javax.swing.JLabel lblTelefono;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlDatosClientes;
    private javax.swing.JTextField tbxContacto;
    private javax.swing.JTextField tbxDireccion;
    private javax.swing.JTextField tbxEmail;
    private javax.swing.JTextField tbxRUC;
    private javax.swing.JTextField tbxRazonSocial;
    private javax.swing.JTextField tbxTelefono;
    // End of variables declaration//GEN-END:variables

}
